package com.homework.ch08;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

/**
 * @author yang
 * @date 2024-03-28
 */
public class TestMain {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) throws Exception {
        //获取 JavaBean 对象的 BeanInfo 对象 - 指向当前类及其父类
        BeanInfo beanInfo = Introspector.getBeanInfo(TestMain.class, Object.class);
        //PropertyDescriptor：属性描述符
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

        //获取属性相关信息
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors){
            System.out.println(propertyDescriptor);
            Class<?> propertyType = propertyDescriptor.getPropertyType();
            System.out.println(propertyType);

            String simpleName = propertyType.getSimpleName();
            String name = propertyType.getName();
            System.out.println("属性类型：" + simpleName);
            System.out.println("属性名称：" + name);
        }
    }
}
